//
//  KKTextView.m
//  KKControlKit
//
//  Created by liaozhenming on 16/10/12.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKTextView.h"

@interface KKTextView() {
    
    UILabel *_placeholderLabel;
    UILabel *_maxTextLengthLabel;
    
    
    BOOL _isListenSelfSuperView;
}

@end

@implementation KKTextView


- (void)setPlaceholder:(NSString *)placeholder {
    
    if (placeholder.length == 0) {
        return;
    }
    _placeholder = placeholder;
    
    if (_placeholderLabel == nil) {
        _placeholderLabel = [[UILabel alloc] initWithFrame: CGRectMake(3, 0, CGRectGetWidth(self.bounds) - 3, 20)];
        _placeholderLabel.textColor = [UIColor lightGrayColor];
        _placeholderLabel.font = [UIFont systemFontOfSize: 13];
        [self addSubview: _placeholderLabel];
        
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(listenTextView:) name:UITextViewTextDidBeginEditingNotification object: self];
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(listenTextView:) name:UITextViewTextDidEndEditingNotification object: self];
    }
    _placeholderLabel.hidden = NO;
    _placeholderLabel.text = placeholder;
}


- (void)setMaxLength:(NSInteger)maxLength {
    
    if (maxLength == 0) {
        return;
    }
    
    if (_maxTextLengthLabel == nil) {
        
        _maxTextLengthLabel = [[UILabel alloc] init];
        _maxTextLengthLabel.backgroundColor = [UIColor clearColor];
        _maxTextLengthLabel.userInteractionEnabled = false;
        _maxTextLengthLabel.textAlignment = NSTextAlignmentRight;
        _maxTextLengthLabel.textColor = [UIColor colorWithWhite:0.75 alpha:1.000];
        _maxTextLengthLabel.font = [UIFont systemFontOfSize: 12];
        
        UIView *superView = self.superview;
        
        if (superView == nil) {
            _isListenSelfSuperView = true;
            [self addObserver: self forKeyPath: @"superview" options: NSKeyValueObservingOptionNew context: nil];
        }
        else {
            
            _maxTextLengthLabel.frame = CGRectMake(CGRectGetMinX(self.frame) - 3, CGRectGetMaxY(self.frame) - 16, CGRectGetWidth(self.frame), 16);
            [superView addSubview: _maxTextLengthLabel];
            
            if (self.autoresizingMask == (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)) {
                
                _maxTextLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
            }
            else {
                
                if ([superView.superview isKindOfClass: [UITableViewCell class]]) {
                    _maxTextLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
                }
                else {
                    _maxTextLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
                }
            }
        }
    }
}

#pragma mark NSNotification methods

- (void)listenTextView:(NSNotification*)notification {
    
    if (notification.object != self) {
        return;
    }
    
    if ([notification.name isEqualToString: UITextViewTextDidBeginEditingNotification]) {
        _placeholderLabel.hidden = true;
    }
    else if ([notification.name isEqualToString: UITextViewTextDidEndEditingNotification]) {
        _placeholderLabel.hidden = self.text.length ? true : false;
    }
    else if ([notification.name isEqualToString: UITextViewTextDidChangeNotification]) {
        
        [self updateTextLength];
    }
}

#pragma mark - Private methods

- (void)updateTextLength {
    
    
    NSInteger length = self.text.length;
    _maxTextLengthLabel.text = [NSString stringWithFormat: @"%@/%@",@(length),@(_maxLength)];
}

#pragma mark -

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString: @"superview"]) {
        
        CGRect converFrame = [self convertRect: self.frame fromView: self.superview];
        _maxTextLengthLabel.frame = CGRectMake(CGRectGetMinX(converFrame) - 3, CGRectGetMaxY(converFrame) - 16, CGRectGetWidth(converFrame), 16);
        [self.superview addSubview: _maxTextLengthLabel];
        
        if (self.autoresizingMask == (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)) {
            
            _maxTextLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        }
        else {
            
            _maxTextLengthLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        }
    }
}

@end
